package com.gitee.wsl.common.chart.render.background.axis.format


import com.gitee.wsl.transform.format.Format
import com.gitee.wsl.jvm.ext.base.DateTimeExt
import java.text.DecimalFormat
import java.util.Date

object LabelFormat {

    val decimalFormat: DecimalFormat by lazy{
        DecimalFormat(DEF_FORMAT)
    }

    /**
     * The default pattern for the [DecimalFormat].
     */
     const val DEF_FORMAT: String = "#.##;−#.##"

    object NUMBER{
        val percentage: Format<Float>
            get() = Format{
            "${it.toInt()}%"
        }

        val decimal: Format<Float>
            get() = Format{
            decimalFormat.format(it)
        }

        fun decimal(digits:Int): Format<Float> {
            val b = StringBuffer()
            for (i in 0 until digits) {
                if (i == 0) {
                    b.append(".")
                }
                b.append("0")
            }

            val mFormat = DecimalFormat("###,###,###,##0$b")
            return Format{
                mFormat.format(it)
            }
        }
    }

    object SIZE{
        fun large(appendix: String = "") = LargeFormatter(appendix)
    }


    object DATA{
        val month: Format<Float>
            get() = Format{
            val timeLong = it.toLong()
            val simpleDateFormat = DateTimeExt.getSafeDateFormat("dd/MM")
            simpleDateFormat.format(Date(timeLong))
        }

        val week: Format<Float>
            get() = Format{
            val timeLong = it.toLong()
            val simpleDateFormat =DateTimeExt.getSafeDateFormat("dd'T'HH:mm")
            simpleDateFormat.format(Date(timeLong))
        }

        fun dataTime(pattern: String): Format<Float> {
            return Format{
                val timeLong = it.toLong()
                val simpleDateFormat = DateTimeExt.getSafeDateFormat(pattern)
                simpleDateFormat.format(Date(timeLong))
            }
        }

        fun candlestickMonth( timestamps: List<Long> = emptyList()): Format<Float> {
            return Format{
                val index = it.toInt()
                val timeLong = timestamps[index]
                val simpleDateFormat =DateTimeExt.getSafeDateFormat("dd'T'HH:mm")
                simpleDateFormat.format(Date(timeLong))
            }
        }
    }

}

val Format.Companion.Label get() = LabelFormat